/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hanze.shoestring.dao.impl;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import nl.hanze.shoestring.dao.GroepskortingDAO;
import nl.hanze.shoestring.domain.Groepskorting;
import nl.hanze.shoestring.domain.Groepskorting_;

/**
 *
 * @author alex
 */
public class GroepskortingDAOImpl extends BaseDAOImpl<Groepskorting> implements GroepskortingDAO {

    public GroepskortingDAOImpl() {
    }

    @Override
    public Groepskorting getKortingByDeelnemers(int numDeelnemers) {
        open();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Groepskorting> query = cb.createQuery(Groepskorting.class);
        Root<Groepskorting> from = query.from(Groepskorting.class);
        Predicate p = cb.and(cb.lessThanOrEqualTo(from.get(Groepskorting_.van), numDeelnemers),
                cb.greaterThanOrEqualTo(from.get(Groepskorting_.tot), numDeelnemers));
        query.where(p);
        TypedQuery typedQuery = em.createQuery(query);
        return (Groepskorting) typedQuery.getSingleResult();
    }
}
